//
//  UINavigationController+DismissPresentedModelViewController.m
//  Mjt
//
//  Created by gao on 16/4/11.
//  Copyright © 2016年 gao. All rights reserved.
//

#import "TMNavigationController.h"

@implementation TMNavigationController
-(void)viewDidAppear:(BOOL)animation{
    [super viewDidAppear:animation];
    //
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
    [self.view.window addGestureRecognizer:recognizer];
    [recognizer setCancelsTouchesInView:NO];
    [recognizer setNumberOfTapsRequired:1];
    [recognizer setDelegate:self];
}
-(void)handleTapBehind:(UITapGestureRecognizer *)sender{
    if (sender.state == UIGestureRecognizerStateEnded) {
        CGPoint location = [sender locationInView:nil];
        if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
            location = CGPointMake(location.y, location.x);
        }
        if (NO == [self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) {
            [self.view.window removeGestureRecognizer:sender];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if(!CGRectEqualToRect(CGRectMake(0, 0, 1024, 768), touch.view.frame) ){
        return NO;
    }
    return YES;
}
@end